﻿Imports System.Collections.ObjectModel
Imports Co9320.NicoLiveService

Namespace ViewModel
    Public Class VoteItemViewModel
        Inherits ViewModelBase

        Private _comments As New ObservableCollection(Of Comment)

        Private _no As Integer
        Public ReadOnly Property No() As Integer
            Get
                Return _no
            End Get
        End Property

        Private _text As String = ""
        Public ReadOnly Property Text() As String
            Get
                Return _text
            End Get
        End Property

        Public ReadOnly Property Count() As Integer
            Get
                Return _comments.Count
            End Get
        End Property

        Public Sub DeleteCommandString(ByVal commandString As String)
            If _text.ToUpperInvariant.Trim.StartsWith(commandString.ToUpperInvariant.Trim) Then
                _text = _text.Trim.Substring(commandString.Trim.Length).Trim
            End If
        End Sub

        Public Sub New(ByVal sourceComment As Comment)
            Me.New(sourceComment, True)
        End Sub

        Public Sub New(ByVal sourceComment As Comment, ByVal andVote As Boolean)
            _no = sourceComment.No
            _text = sourceComment.Text.Replace(vbCr, "").Replace(vbLf, "").Trim
            If andVote Then
                _comments.Add(sourceComment)
            End If
        End Sub

        Public Sub Increment(ByVal sourceComment As Comment)

            Dim comments = From c In _comments Where c.UserId = sourceComment.UserId
            If comments.Count = 0 Then
                _comments.Add(sourceComment)
                OnPropertyChanged("Count")
            End If

        End Sub

        Public Sub Decrement(ByVal sourceComment As Comment)

            Dim comments = From c In _comments Where c.UserId = sourceComment.UserId
            If comments.Count = 1 Then
                _comments.Remove(comments.SingleOrDefault)
                OnPropertyChanged("Count")
            End If

        End Sub

        Public Sub Clear()
            _comments.Clear()
        End Sub

        Public Function ContainUser(ByVal userId As String) As Boolean

            For Each c In _comments
                If c.UserId = userId Then
                    Return True
                End If
            Next
            Return False

        End Function

    End Class

End Namespace
